//
//  main.c
//  OSD_test
//
//  Created by gehj on 2023/2/9.
//
/**
YU16(YUV422 planar )
 yyyy
 yyyy
 uuuu
 vvvv
 
 
 YUV422SP(NV16)
 yyyy
 yyyy
 uvuv
 uvuv
 
 

 思路：
 1，RBG24图片:mask文字图片----->YUV422 planar(YU16)
 2，NV16(背景图片) -->YUV422 planar
 3，保证上面二者都是YUV422 planar --> 各通道混合叠加
     3.1  加x,y 处理
     3.2  透明通道混合处理
 4，还原为NV16
 
 工具类： NV16转YUV422 planar之间互转
        YUV422 planar提取YUV三轴
        RGB 转YUV422planar
 
 */

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/time.h>

#include "rgb2yuv.h"
#include "yuvconvert.h"
#include "overlay.h"


int main(int argc, const char * argv[]) {
    
    
//整体测试
#if 1
    //1.1 rgb转YV16
    //osd（mask层）小图长宽
    int w_mask = 860;
    int h_mask = 454;
    
    //背景图片长宽
    int w_bg = 1920;
    int h_bg = 1080;
    
   
    unsigned char *prgb888 = NULL;
    unsigned char *pyuv_mask = NULL;
    unsigned char *pyuvsp_bg = NULL;
    unsigned char *pyuvp_bg = NULL;
    unsigned char *pyuvsp_dest = NULL;
    
    FILE *pfile_rgb = NULL;
    FILE *pfile_yuv = NULL;
    FILE *pfile_sp_bg = NULL;
    FILE *pfile_sp_dest = NULL; //输出不能free掉
    
    
    struct timeval starttime;
    struct timeval endtime;
    
    prgb888 = malloc(3*w_mask*h_mask);
    pyuv_mask = malloc(2*w_mask*h_mask);
    pyuvp_bg = malloc(2*w_bg*h_bg);
    pyuvsp_bg = malloc(2*w_bg*h_bg);
    pyuvsp_dest = malloc(2*w_bg*h_bg);
    
    
    pfile_rgb = fopen("/Users/gehj/Desktop/test-case/2-rgb2YU16/hdcvt_860_454_rgb24.raw", "rb");
    
    pfile_sp_bg = fopen("/Users/gehj/Desktop/test-case/1-NV16ToYU16/BG_1920_1080_YUV422_NV16.raw", "rb");
    
    pfile_sp_dest = fopen("/Users/gehj/Desktop/mux_out.raw", "wb");
    
    fread(prgb888, 1, 3*w_mask*h_mask, pfile_rgb);
    fread(pyuvsp_bg, 1, 2*w_bg*h_bg, pfile_sp_bg);
    
    
    printf("start\n");
    gettimeofday(&starttime, NULL);
    //1.1 rgb24转YU16
    rgb888_to_yuv422p(prgb888, pyuv_mask, w_mask, h_mask);
    //1.2 NV61转YU16
    NV16_to_YU16(pyuvsp_bg,pyuvp_bg,w_bg, h_bg);
    
    //2 进行覆盖
    YV16_overlay_transparent(pyuv_mask,
                             pyuvp_bg,
                             w_mask, h_mask,
                             w_bg,h_bg,
                             1060,626,
                             0.6);
    //3 再转回原图
    YU16_to_NV16(pyuvsp_dest,pyuvp_bg,w_bg, h_bg);
    
    
    gettimeofday(&endtime, NULL);
    
    fwrite(pyuvsp_dest, 1, 2*w_bg*h_bg, pfile_sp_dest);
    
    
    
    
    printf("Start time: %ld us\n", starttime.tv_usec);
            printf("End time: %ld us\n", endtime.tv_usec);
            if (endtime.tv_usec > starttime.tv_usec)
            {
                printf("process time: %ld us\n", endtime.tv_usec - starttime.tv_usec);
            }
    
            if (pfile_yuv)
            {
                fclose(pfile_yuv);
                pfile_yuv = NULL;
            }

            if (pfile_rgb)
            {
                fclose(pfile_rgb);
                pfile_rgb = NULL;
            }
            
            if (pyuv_mask)
            {
                free(pyuv_mask);
                pyuv_mask = NULL;
            }

            if (prgb888)
            {
                free(prgb888);
                prgb888 = NULL;
            }
            
            if(pyuvsp_bg)
            {
                free(pyuvsp_bg);
                pyuvsp_bg = NULL;
            }
            
            if(pyuvp_bg)
            {
                free(pyuvp_bg);
                pyuvp_bg = NULL;
            }
            
            if(pyuvsp_dest)
            {
                free(pyuvsp_dest);
                pyuvsp_dest = NULL;
            }
    
           
    
    
    
    
#endif
    
/**
#if 0
    //小图叠加透明层
    FILE* maskFile = NULL;//小图
    FILE* Bgfile= NULL;//背景图
    
    maskFile = fopen("/Users/gehj/Desktop/test-case/3-overlay/hdcvt_860_454_yv16.raw", "rb");
    Bgfile = fopen("/Users/gehj/Desktop/test-case/3-overlay/Convert_YU16_1080P.raw", "rb");
    
    int sw = 860, sh=454; //小图在上面叠加
    int bw = 1920, bh = 1080;
    
    unsigned char* yuvs = NULL ;
    unsigned char* yuvb = NULL;
    yuvs = (unsigned char*) malloc (sizeof(unsigned char)*sw*sh*2);
    yuvb = (unsigned char*) malloc (sizeof(unsigned char)*bw*bh*2);
    fread (yuvs,1,sw*sh*2,maskFile);
    fread (yuvb,1,bw*bh*2,Bgfile);
    

    
    YV16_overlay_transparent(yuvs,
                             yuvb,
                             sw, sh,
                             bw,bh,
                             50,100,
                             0xa0);
    FILE* fpout = NULL;
    fpout = fopen("/Users/gehj/Desktop/mux_out.raw", "wb");
    fwrite(yuvb, 1, bw*bh*2, fpout);
    fclose(fpout);
    fclose(maskFile);
    fclose(Bgfile);
    free(yuvs);
    free(yuvb);
#endif
 
#if 0
    //小图叠加不透明
    FILE* maskFile = NULL;//小图
    FILE* Bgfile= NULL;//背景图
    
    maskFile = fopen("/Users/gehj/Desktop/test-case/3-overlay/hdcvt_860_454_yv16.raw", "rb");
    Bgfile = fopen("/Users/gehj/Desktop/test-case/3-overlay/Convert_YV16_1080P.raw", "rb");
    
    int sw = 860, sh=454; //小图在上面叠加
    int bw = 1920, bh = 1080;
    
    unsigned char* yuvs = NULL ;
    unsigned char* yuvb = NULL;
    yuvs = (unsigned char*) malloc (sizeof(unsigned char)*sw*sh*2);
    yuvb = (unsigned char*) malloc (sizeof(unsigned char)*bw*bh*2);
    fread (yuvs,1,sw*sh*2,maskFile);
    fread (yuvb,1,bw*bh*2,Bgfile);
    
    
    YV16_overlay(yuvs,
                 yuvb,
                 sw, sh,
                 bw,bh,50,100);
    
    FILE* fpout = NULL;
    fpout = fopen("/Users/gehj/Desktop/mux_out.raw", "wb");
    fwrite(yuvb, 1, bw*bh*2, fpout);
    fclose(fpout);
    fclose(maskFile);
    fclose(Bgfile);
    free(yuvs);
    free(yuvb);
    
#endif
    
#if 0
    //测试YU16转NV16
    int w = 1920;
    int h = 1080;
   
    unsigned char *pyuvsp = NULL;
    unsigned char *pyuvp = NULL;
    FILE *pfile_p = NULL;
    FILE *pfile_sp = NULL;
    
    
    pyuvp = malloc(2*w*h);
    pyuvsp = malloc(2*w*h);
    
    pfile_p = fopen("/Users/gehj/Desktop/test-case/4-YU16ToNV61/result_1080P_YU16.raw","rb");
    pfile_sp = fopen("/Users/gehj/Desktop/mux_out.raw", "wb");
    

    fread(pyuvp, 1, 2*w*h, pfile_p);
    
    struct timeval starttime;
    struct timeval endtime;
     
    
    gettimeofday(&starttime, NULL);
    YU16_to_NV16(pyuvsp,pyuvp,w, h);

    gettimeofday(&endtime, NULL);
    
    fwrite(pyuvsp, 1, 2*w*h, pfile_sp);
    
    printf("Start time: %ld us\n", starttime.tv_usec);
    printf("End time: %ld us\n", endtime.tv_usec);
    if (endtime.tv_usec > starttime.tv_usec)
    {
        printf("process time: %ld us\n", endtime.tv_usec - starttime.tv_usec);
    }
    //TODO free一堆东西
#endif
    
#if 0
    //测试NV16转YU16
    int w = 1920;
    int h = 1080;
    unsigned char *pyuvsp = NULL;
    unsigned char *pyuvp = NULL;
    FILE *pfile_sp = NULL;
    FILE *pfile_p = NULL;
    
    pyuvp = malloc(2*w*h);
    pyuvsp = malloc(2*w*h);
    
    
    pfile_sp = fopen("/Users/gehj/Desktop/test-case/1-NV61ToYU16/BG_1920_1080_YUV422_NV16.raw", "rb");
    pfile_p = fopen("/Users/gehj/Desktop/mux_out.raw","wb");

    fread(pyuvsp, 1, 2*w*h, pfile_sp);
    
    struct timeval starttime;
    struct timeval endtime;
     
    
    gettimeofday(&starttime, NULL);
    NV16_to_YU16(pyuvsp,pyuvp,w, h);
//  NV61_to_YV16(pyuvsp,pyvup,w, h);
    
//  printf("Hello, World!\n");
    gettimeofday(&endtime, NULL);
    
    fwrite(pyuvp, 1, 2*w*h, pfile_p);
    
    printf("Start time: %ld us\n", starttime.tv_usec);
    printf("End time: %ld us\n", endtime.tv_usec);
    if (endtime.tv_usec > starttime.tv_usec)
    {
        printf("process time: %ld us\n", endtime.tv_usec - starttime.tv_usec);
    }
    //TODO free一堆东西
#endif
    
#if 0
    //rgb24转YU16
//        int w = 1280;
//        int h = 720;
        int w = 450; //根据实际情况调整
        int h = 324;
        
        unsigned char *prgb888 = NULL;
        unsigned char *pyuv = NULL;
        FILE *pfile_rgb = NULL;
        FILE *pfile_yuv = NULL;
    
        struct timeval starttime;
        struct timeval endtime;
    
        prgb888 = malloc(3*w*h);
        pyuv = malloc(2*w*h);
    //路径根据实际情况进行调整
    pfile_rgb = fopen("/Users/gehj/Desktop/logo_material/even/rgb_even2raw/openwrt-black_450_324.raw", "rb");
    //路径根据实际情况进行调整
        pfile_yuv = fopen("/Users/gehj/Desktop/mux_out.raw","wb");
    
        fread(prgb888, 1, 3*w*h, pfile_rgb);
        
        printf("rgb888 to yvu422p\n");
        gettimeofday(&starttime, NULL);
        rgb888_to_yuv422p(prgb888, pyuv, w, h);
        gettimeofday(&endtime, NULL);
        fwrite(pyuv, 1, 2*w*h, pfile_yuv);
        
        printf("Start time: %ld us\n", starttime.tv_usec);
        printf("End time: %ld us\n", endtime.tv_usec);
        if (endtime.tv_usec > starttime.tv_usec)
        {
            printf("process time: %ld us\n", endtime.tv_usec - starttime.tv_usec);
        }
    
        if (pfile_yuv)
        {
            fclose(pfile_yuv);
            pfile_yuv = NULL;
        }

        if (pfile_rgb)
        {
            fclose(pfile_rgb);
            pfile_rgb = NULL;
        }
        
        if (pyuv)
        {
            free(pyuv);
            pyuv = NULL;
        }

        if (prgb888)
        {
            free(prgb888);
            prgb888 = NULL;
        }
        
    
#endif
    
#if 0
    //最原始的覆盖算法，只有y轴
    FILE* maskFile = NULL;//小图
//    maskFile = fopen("/Users/gehj/Desktop/OSD-gehj/16x8x1B.raw", "rb");
    maskFile = fopen("/Users/gehj/Desktop/OSD-gehj/3_24*38.RAW", "rb");
    FILE* Bgfile;//背景图
//    Bgfile = fopen("/Users/gehj/Desktop/OSD-gehj/3_24*38.RAW", "rb");
    Bgfile = fopen("/Users/gehj/Desktop/OSD-gehj/Y_single_1080P.RAW", "rb");
//    int sw = 16, sh=8; //小图在上面叠加
    int sw = 24, sh=38; //小图在上面叠加
//    int bw = 24, bh = 38;//大图背景图
    int bw = 1920, bh = 1080;
    
    char* yuvs = NULL ;
    char* yuvb = NULL;
    yuvs = (char*) malloc (sizeof(char)*sw*sh);
    yuvb = (char*) malloc (sizeof(char)*bw*bh);
    fread (yuvs,1,sw*sh,maskFile);
    fread (yuvb,1,bw*bh,Bgfile);
    //y_b，y_s改变指针所存的内容
    char* y_s = yuvs;
    char* y_b = yuvb;//yuvb为原始指针，后面输出要使用
   
    //从图像左上角开始进行y覆盖
    //xy坐标确定：197000/width = y 197000%width = x;
    for (int i = 0; i < sh; i++)
    {
        memcpy(y_b+197000, y_s, sw);//mask覆盖背景
        //换行，指针偏移到行首
        y_b = y_b + bw;
        y_s = y_s + sw;
      
    }
    
    FILE* fpout = NULL;
    fpout = fopen("/Users/gehj/Desktop/mux_out.raw", "wb");
    fwrite(yuvb, 1, bw*bh, fpout);

    FILE* fpout = NULL;
    fpout = fopen("/Users/gehj/Desktop/mux_out.raw", "wb");
    fwrite(digital_2, 1, 24*38, fpout);
    fclose(fpout);
    fclose(maskFile);
    fclose(Bgfile);
    free(yuvs);
    free(yuvb) ;
#endif
    
#if 0
    //osd覆盖=坐标可以移动 没有透明度；
    FILE* maskFile = NULL;//小图
//    maskFile = fopen("/Users/gehj/Desktop/OSD-gehj/16x8x1B.raw", "rb");
    maskFile = fopen("/Users/gehj/Desktop/OSD-gehj/3_24*38.RAW", "rb");
    FILE* Bgfile;//背景图
//    Bgfile = fopen("/Users/gehj/Desktop/OSD-gehj/3_24*38.RAW", "rb");
    Bgfile = fopen("/Users/gehj/Desktop/OSD-gehj/Y_single_1080P.RAW", "rb");
//    int sw = 16, sh=8; //小图在上面叠加
    int sw = 24, sh=38; //小图在上面叠加
//    int bw = 24, bh = 38;//大图背景图
    int bw = 1920, bh = 1080;
    
    unsigned char* yuvs = NULL ;
    unsigned char* yuvb = NULL;
    yuvs = (unsigned char*) malloc (sizeof(unsigned char)*sw*sh);
    yuvb = (unsigned char*) malloc (sizeof(unsigned char)*bw*bh);
    fread (yuvs,1,sw*sh,maskFile);
    fread (yuvb,1,bw*bh,Bgfile);
    
    
    Y_xy_overlay(yuvs,
                 yuvb,
                 24, 38,
                 1920,1080,0,0);
    
    FILE* fpout = NULL;
    fpout = fopen("/Users/gehj/Desktop/mux_out.raw", "wb");
    fwrite(yuvb, 1, bw*bh, fpout);
    fclose(fpout);
    fclose(maskFile);
    fclose(Bgfile);
    free(yuvs);
    free(yuvb) ;
    
#endif
  // printf("Hello, World!\n");
  // unsigned char * buffer = NULL;
  // long Y_channel =1280*720;
  //buffer = (char*) malloc (sizeof(char)*Y_channel);
  //fread (buffer,1,Y_channel,infile);
    */
    return 0;
   
  
    
}
